# -*- coding:utf-8 -*-
from . import file_blu
from flask import request
from info import constants
from urllib.parse import urljoin


@file_blu.route("/upload", methods=["POST"])
async def upload():
    # 校验参数
    try:
        file = request.files.get("avatar").read()
    except Exception as e:
        current_app.logger.error(e)
        return info_jsonfy(RET.PARAMERR, "参数错误")

    # 上传至七牛云
    fileUrl = await storage(file)
    if not fileUrl:
        return info_jsonfy(RET.THIRDERR, "上传失败")
    data = {
        "modify_avatar_url": urljoin(constants.QINIU_DOMIN_PREFIX, fileUrl)
    }
    return info_jsonfy(RET.OK, "上传成功", data)
